package com.sise.parkinglot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sise.parkinglot.bean.CodeMsg;
import com.sise.parkinglot.entity.VipLog;
import com.sise.parkinglot.mapper.VipLogMapper;
import com.sise.parkinglot.service.IVipLogService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author ZJ
 */
@Service
public class VipLogServiceImpl extends ServiceImpl<VipLogMapper, VipLog> implements IVipLogService {

    @Resource
    private VipLogMapper vipLogMapper;

    @Override
    public CodeMsg<List<VipLog>> queryVipLogList(Map<String, Object> map) {
        if (map.isEmpty()) {
            return new CodeMsg<List<VipLog>>().setCode(400).setMsg("参数缺失");
        }
        Page<VipLog> page = new Page<>((int) map.get("current"), (int) map.get("pageSize"));
        QueryWrapper<VipLog> VipLogQueryWrapper = new QueryWrapper<>();
        if (map.get("id") != null && !map.get("id").equals("")) {
            VipLogQueryWrapper.eq("id", map.get("id"));
        }
        if (map.get("userId") != null && !map.get("userId").equals("")) {
            VipLogQueryWrapper.eq("user_id", map.get("userId"));
        }
        if (map.get("vip") != null && !map.get("vip").equals("")) {
            VipLogQueryWrapper.eq("vip", map.get("vip"));
        }
        if (map.get("updateTime") != null && !map.get("updateTime").equals("")) {
            String updateTime = (String) map.get("updateTime");
            VipLogQueryWrapper.likeRight("update_time", updateTime.split(" ")[0]);
        }
        VipLogQueryWrapper.lambda().orderByDesc(VipLog::getUpdateTime);
        IPage<VipLog> vipLogPage = vipLogMapper.selectPage(page, VipLogQueryWrapper);
        List<VipLog> vipLogList = vipLogPage.getRecords();
        return new CodeMsg<List<VipLog>>().setCode(200).setMsg(String.valueOf(vipLogPage.getTotal())).setData(vipLogList);
    }
}
